package com.todod.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
import org.springframework.web.multipart.MultipartFile;

/**
 * excel读写工具类
 * 
 * @author sun.kai 2016年8月21日
 */
public class ReadFile {

	public static String getContent2003(String url) {
		InputStream is = null;
		WordExtractor ex = null;
		try {
			is = new FileInputStream(new File(url));
			ex = new WordExtractor(is);
			String content = ex.getText();

			return content;
		} catch (Exception e) {
		} finally {
			try {
				if (is != null)
					is.close();
				if (ex != null)
					ex.close();
			} catch (IOException e) {
			}
		}
		return null;
	}

	public static String getContent2007(String url) {
		FileInputStream is = null;
		XWPFDocument doc = null;
		XWPFWordExtractor ex = null;
		try {
			is = new FileInputStream(new File(url));
			doc = new XWPFDocument(is);
			ex = new XWPFWordExtractor(doc);

			String content = ex.getText();
			return content;
		} catch (Exception e) {
		} finally {
			try {
				if (ex != null)
					ex.close();
				if (doc != null)
					doc.close();
				if (is != null)
					is.close();
			} catch (IOException e) {
			}
		}
		return null;
	}

	/**
	 * 传入txt路径读取txt文件
	 * 
	 * @param txtPath
	 * @return 返回读取到的内容
	 */
	public static String readTxt(String txtPath) {
		File file = new File(txtPath);
		if (file.isFile() && file.exists()) {
			try {
				FileInputStream fileInputStream = new FileInputStream(file);
				InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

				StringBuffer sb = new StringBuffer();
				String text = null;
				while ((text = bufferedReader.readLine()) != null) {
					sb.append(text);
				}
				return sb.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 读取 PDF文本内容
	 *
	 * @Param: MultipartFile
	 * @return: pdf文本内容
	 */
	public static String readPdf(MultipartFile file) {
		StringBuilder content = new StringBuilder();
		try {
			InputStream is = file.getInputStream();
			PDFParser parser = new PDFParser(new RandomAccessBuffer(is));
			parser.parse();
			// 读取文本内容
			PDDocument document = parser.getPDDocument();
			// 获取页码
			int pages = document.getNumberOfPages();
			PDFTextStripper stripper = new PDFTextStripper();
			// 设置按顺序输出
			stripper.setSortByPosition(true);
			stripper.setStartPage(1);
			stripper.setEndPage(pages);
			content.append(stripper.getText(document));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return content.toString();
	}

	// 读取ppt文件内容#
	public static String readPPT(MultipartFile file) {
		if (file.isEmpty())
			return "";
		StringBuilder content = new StringBuilder();
		try {
			InputStream is = file.getInputStream();
			HSLFSlideShow hslfSlideShow = new HSLFSlideShow(is);
			List<HSLFSlide> slides = hslfSlideShow.getSlides();
			SlideShowExtractor slideShowExtractor = new SlideShowExtractor(hslfSlideShow);

			for (HSLFSlide slide : slides) {
				content.append(slideShowExtractor.getText(slide));
			}
			slideShowExtractor.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content.toString();
	}

	// 读取pptx文件内容#
	public static String readPPTX(MultipartFile file) {
		if (file.isEmpty())
			return "";
		StringBuffer content = new StringBuffer();
		try {
			InputStream is = file.getInputStream();
			XMLSlideShow xmlSlideShow = new XMLSlideShow(is);
			List<XSLFSlide> slides = xmlSlideShow.getSlides(); // 获得每一张幻灯片
			for (XSLFSlide slide : slides) {
				CTSlide rawSlide = slide.getXmlObject();
				CTGroupShape spTree = rawSlide.getCSld().getSpTree();
				List<CTShape> spList = spTree.getSpList();
				for (CTShape shape : spList) {
					CTTextBody txBody = shape.getTxBody();
					if (null == txBody) {
						continue;
					}
					List<CTTextParagraph> pList = txBody.getPList();
					for (CTTextParagraph textParagraph : pList) {
						List<CTRegularTextRun> textRuns = textParagraph.getRList();
						for (CTRegularTextRun textRun : textRuns) {
							content.append(textRun.getT());
						}
					}
				}
			}
			xmlSlideShow.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content.toString();
	}

	public static String readPdf(FileInputStream fileInputStream) {
		StringBuilder content = new StringBuilder();
		try {
			PDFParser parser = new PDFParser(new RandomAccessBuffer(fileInputStream));
			parser.parse();
			// 读取文本内容
			PDDocument document = parser.getPDDocument();
			// 获取页码
			int pages = document.getNumberOfPages();
			PDFTextStripper stripper = new PDFTextStripper();
			// 设置按顺序输出
			stripper.setSortByPosition(true);
			stripper.setStartPage(1);
			stripper.setEndPage(pages);
			content.append(stripper.getText(document));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return content.toString();
	}

}